package com.leyou.item.service.impl;

import com.leyou.common.enums.ExceptionEnum;
import com.leyou.common.exceptions.LyException;
import com.leyou.common.utils.BeanHelper;
import com.leyou.item.dto.SpecGroupDTO;
import com.leyou.item.dto.SpecParamDTO;
import com.leyou.item.entity.SpecGroup;
import com.leyou.item.entity.SpecParam;
import com.leyou.item.mapper.GroupMapper;
import com.leyou.item.mapper.ParamMapper;
import com.leyou.item.service.ISpecService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * 处理请求和响应的方法
 *
 * @version V1.0
 * @package com.leyou.item.service.impl
 * @description:
 * @author: pc
 * @date: 2019/6/26 17:18
 */

@Service("specService")
public class SpecServiceImpl implements ISpecService {

    @Autowired
    private GroupMapper groupMapper;
    /**
     *根据分类Id查询规格及规格参数
     */
    @Override
    public List<SpecGroupDTO> querySpecsById(Long cid) {
        // 查询规格组
        List<SpecGroupDTO> groupList = queryGroupByCategory(cid);
        // 查询分类下所有规格参数
        List<SpecParamDTO> params = queryGroupByParam(null, cid, null);
        Map<Long, List<SpecParamDTO>> map = params.stream().collect(Collectors.groupingBy(SpecParamDTO::getGroupId));
        /*//对根据groundId对params进行分组
        Map<Long,List<SpecParamDTO>> map = new HashMap<>();
        //遍历params
        for (SpecParamDTO param : params) {
            //.containsKey表示，判断当前map的key中是否包含了参数中的值
            if (!map.containsKey(param.getGroupId())) {
                //如果map中的key,没有param集合中GroupId，表示当前list是第一组数据
                //所以，添加map数据，key为当前list的GroupId，值为一个新建的list集合
                map.put(param.getGroupId(),new ArrayList<>());
            }
            //如果map中已经有相同的key了，就调取并添加值；
            map.get(param.getGroupId()).add(param);
        }*/
        for (SpecGroupDTO groupDTO : groupList) {
            groupDTO.setParams(map.get(groupDTO.getId()));
        }
        return groupList;
    }

    /**
     * 根据Cid查询规格
     */
    @Override
    public List<SpecGroupDTO> queryGroupByCategory(Long cid) {

        SpecGroup specGroup = new SpecGroup();

        specGroup.setCid(cid);

        List<SpecGroup> list = groupMapper.select(specGroup);

        if (CollectionUtils.isEmpty(list)) {
            throw new LyException(ExceptionEnum.SPEC_NOT_FOUND);
        }

        List<SpecGroupDTO> result = BeanHelper.copyWithCollection(list, SpecGroupDTO.class);
        return result;
    }

    @Autowired
    private ParamMapper paramMapper;

    @Override
    public List<SpecParamDTO> queryGroupByParam(Long gid, Long cid, Boolean searching) {
        //gid与cid必须有一个不能为空
        if (gid == null && cid == null && searching == null) {
            throw new LyException(ExceptionEnum.INVALID_PARAM_ERROR);
        }
        //创建与数据库连接的对象类
        SpecParam specParam = new SpecParam();
        //将groud_id输入类中
        specParam.setGroupId(gid);
        specParam.setCid(cid);
        specParam.setSearching(searching);
        //使用通用mapper查询数据
        List<SpecParam> list = paramMapper.select(specParam);
        //判空
        if (CollectionUtils.isEmpty(list)) {
            throw new LyException(ExceptionEnum.SPEC_NOT_FOUND);
        }
        //数据类型转换
        return BeanHelper.copyWithCollection(list, SpecParamDTO.class);
    }
}
